import 'package:flutter/material.dart';
import 'package:wanandroid/common/common_index.dart';
import 'page_index.dart';

class _Page {
  final String labelId;

  _Page(this.labelId);
}

final List<_Page> _allPages = <_Page>[
  new _Page(StringIds.titleHome),
  new _Page(StringIds.titleRepos),
  new _Page(StringIds.titleEvents),
  new _Page(StringIds.titleSystem),
];

class MainPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: _allPages.length,
      child: Scaffold(
        appBar: MyAppBar(
          leading: Container(
            decoration: BoxDecoration(
              shape: BoxShape.circle,
              image: DecorationImage(
                  image: AssetImage(Utils.getImgPath('ali_connors'))),
            ),
          ),
          centerTitle: true,
          title: MainHeaderTab(),
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.search),
              onPressed: () {
                NavigatorUtil.push(context, (ctx) => SplashPage());
              },
            )
          ],
        ),
        body: MyViewPager(),
        drawer: Drawer(
          child: MainPageDrawer(),
        ),
      ),
    );
  }
}

class MainHeaderTab extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return TabBar(
      isScrollable: true,
      labelPadding: EdgeInsets.all(12.0),
      indicatorSize: TabBarIndicatorSize.label,
      tabs: _allPages.map((_Page page) =>
          Tab(text: IntlUtil.getString(context, page.labelId),)
      ).toList(),
    );
  }
}

class MyViewPager extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return TabBarView(
      children: _allPages.map((page) {
        return Builder(builder: (BuildContext context) {
          return Container(
            key: ValueKey(IntlUtil.getString(context, page.labelId)),
            child: Center(
              child: Text(IntlUtil.getString(context, page.labelId),
                style: TextStyle(fontSize: 32.0),
                textAlign: TextAlign.center,),
            ),
          );
        });
      }).toList()
    );
  }

}
